import {jidongcharing,jumpMiniProgram,commonParams,getIntegralUrl,IMG_URL,halfScreenUrl} from '../env.js';
//语音相关
var audio=uni.createInnerAudioContext();
audio.volume=1;

const utils = {
	toast: function(text,success) {
		uni.showToast({
			title: text || "出错啦~",
			icon: success ? 'success' : 'none',
			duration:2000,
		});
	},
	toastCallback:(text,fn,isSuccess)=>{
		uni.showToast({
			title: text || "出错啦~",
			icon: isSuccess ? 'success' : 'none',
			mask:true,
		});
		clearTimeout(timer);
		let timer=setTimeout(()=>{
			fn();
		},2000);
	},
	modal: function(obj) {
		uni.showModal({
			title: obj.title || '温馨提示',
			content: obj.content,
			showCancel:(typeof obj.showCancel)==='boolean'?obj.showCancel:true,
			cancelColor: obj.cancelColor || "#555",
			cancelText:obj.cancelText || '取消',
			confirmColor:obj.confirmColor || "#0da297",
			confirmText: obj.confirmText || "确定",
			success(res) {
				if (res.confirm) {
					obj.callback && obj.callback(true)
				} else {
					obj.callback && obj.callback(false)
				}
			}
		})
	},
	back:function(delta){
		uni.navigateBack({
		    delta:delta||1
		});
	},
	isPhone: function(value) {
		return /^[1][3-9][0-9]{9}$/.test(value);
	},
	isAllEmpty:function(value){
		if(value=='' ||  /^\s+$/.test(value) || value==null){
			return true;
		}else{
			return false;
		}
	},
	oldCommonJump:function(data){
		let obj={
			nothing:data.originalData.type=="0",
			isDisable:data.originalData.is_enter == '1',
			disableTxt:data.originalData.msg,
			appId:data.originalData.car_shop_appid,
			appPath:data.originalData.url_platform,
			half_screen:data.originalData.half_screen,
		};
		if(data.originalData.is_self=='2'){
			obj.isWebview=false;
			obj.url=data.originalData.self_url;
		}else if(data.originalData.is_self=='1'){
			obj.isWebview=true;
			obj.url=data.originalData.url;
		};		
		if(obj.nothing){
			return false;
		};
		//禁用了
		if(obj.isDisable){
			this.toast(obj.disableTxt);
			return false;
		};
		if(obj.half_screen == 1 || obj.half_screen == 3){
			utils.halfScreen(obj.appId,obj.appPath);
			return false;
		}
		//跳转至小程序
		if(obj.appId){
			
			uni.navigateToMiniProgram({
				appId:obj.appId,
				path: obj.appPath,
				success(res) {
					uni.request({
					    url:jumpMiniProgram,
						method:'POST',
						data:{
							userid_locked: uni.getStorageSync('userId'),
							url:obj.appPath,
							...commonParams,
						},
					    success: res=>{
							uni.request({
							    url:jidongcharing,
								method:'POST',
								data:{appid:obj.appId,userid_locked: uni.getStorageSync('userId'),
							   appPath:obj.appPath,
							...commonParams,},
							    success: res=>{
									console.log('调用成功');
								},
								fail:err=>{}
							});
							
						},
						fail:err=>{}
					});
				}
			})
			return false;
		};
		//webview
		if(obj.isWebview){
			
			//积分接口。
			uni.request({
			    url:getIntegralUrl,
				method:'POST',
				data:{
					userid_locked: uni.getStorageSync('userId'),
					type:data.from,
					content:JSON.stringify(data.originalData),
					url:obj.url,
					...commonParams,
				},
			    success: res=>{

					if(res.data.data.img){
						uni.showToast({
							title:res.data.data.tips,
							icon:'none',
							image:'/static/images/integral.png',
						});
					};
					
				},
				fail:err=>{}
			});
			//跳转至h5页面。
			this.jumpWebview(obj.url);
		}else{
			//跳转至小程序内页。
			uni.navigateTo({
			    url:obj.url
			});
		};
	},

	jumpWebview:function(url){
		if(url.indexOf('https://api.cd1a.cn')!=-1 || url.indexOf('https://test.api.cd1a.cn')!=-1){
			//我们自己的h5链接
			uni.navigateTo({
				url:"/pages/webview/index?url="+encodeURIComponent(JSON.stringify(url))
			});
		}else{
			//第三方h5链接，
			uni.setStorageSync('specialUrl',encodeURIComponent(JSON.stringify(url)));
			uni.navigateTo({
			    url:"/pages/webview/index?special=1"
			});
		};
	},
	getQueryStringByName:function(url,name){
		var result = url.match(new RegExp("[\?\&]" + name+ "=([^\&]+)","i"));
		if(result == null || result.length < 1){
			return "";
		};
		return result[1];
	},
	//公众号组件相关
	isScene(scene){
		return [1011].includes(scene);
	},
	//语音
	playAudio(src){
		audio.src=src;
		audio.play();
	},
	//语音
	stopAudio(){
		audio.stop();
	},
	//半屏小程序
	halfScreen(appId,path){
		//*微信
		// #ifdef	MP-WEIXIN
		if(uni.openEmbeddedMiniProgram){
			uni.openEmbeddedMiniProgram({
				appId:appId,
				path:path,
				extraData: {
						'data1': 'test'
					},
				success(){
					uni.request({
					    url: halfScreenUrl,
						data:{
							userid_locked: uni.getStorageSync('userId'),
							...commonParams,
						},
						success: res=>{
							uni.request({
							    url:jidongcharing,
								method:'POST',
								data:{appid:appId,userid_locked: uni.getStorageSync('userId'),
							   appPath:path,
							...commonParams,},
							    success: res=>{
								},
								fail:err=>{}
							});
							
						},
					})
				}
				
			});
		}else {
			uni.navigateToMiniProgram({
				appId: appId,
				path: path,
				extraData: {
					'data1': 'test'
				},
				success(res) {
			  console.log('跳转成功');
				}
			}) 
		}
		
		// #endif
	}
	
	
	
}
export default utils;